#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#
# Copyright 2013 suliu.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#codeby     道长且阻
#email      ydhcui@suliu.net/QQ664284092
#website    http://www.suliu.net

from peewee import *
import datetime,util

USERDATA = SqliteDatabase(util.getpath('walle.db'))
USERDATA.connect()

class BaseModel(Model):
    class Meta:
        database = USERDATA

class Setting(BaseModel):
    id = IntegerField(primary_key=True)
    username = CharField()
    password = CharField()
    botname = CharField('WALLE')
    isautore = BooleanField(default=True)
    issyslog = BooleanField(default=True)
    ischatlog = BooleanField(default=True)
    startkey = CharField(default='WALLE')
    startmsg = CharField(default='stoprobot')
    endkey = CharField(default='哈哈哈哈 超级无敌的walle回来啦~')
    endmsg = CharField(default='啊~我还会回来的。。。。')

class User(BaseModel):
    id = IntegerField(primary_key=True)
    userqq = CharField(default='')
    qq = CharField()
    uin = CharField()
    nick = CharField(default='')
    face = CharField(default='')
    flag = CharField(default='')

class Group(BaseModel):
    id = IntegerField(primary_key=True)
    users = ForeignKeyField(User)
    uin = CharField()
    nick = CharField()

class Userai(BaseModel):
    id = IntegerField(primary_key=True)
    instr = CharField()
    outstr = CharField()
    createdate = DateTimeField(default=datetime.datetime.now)

class Aikey(BaseModel):
    id = IntegerField(primary_key=True)
    data = CharField()
    createdate = DateTimeField(default=datetime.datetime.now)

class Joke(BaseModel):
    id = IntegerField(primary_key=True)
    data = TextField()
    type = CharField(default='')
    isread = BooleanField(default=False)
    createdate = DateTimeField(default=datetime.datetime.now)

class Quotes(BaseModel):
    id = IntegerField(primary_key=True)
    data = CharField()
    type = CharField(default='')
    isread = BooleanField(default=False)
    createdate = DateTimeField(default=datetime.datetime.now)

class Logs(BaseModel):
    id = IntegerField(primary_key=True)
    fromqq = CharField()
    toqq = CharField()
    content = TextField()
    createdate = DateTimeField(default=datetime.datetime.now)

class Qusers(BaseModel):
    id = IntegerField(primary_key=True)
    username = CharField()
    password = CharField()
    nick = CharField(default='NULL')


if __name__=='__main__':
    import util ,json ,time

    def create_table():
        Qusers.create_table(fail_silently=True)
        Setting.create_table(fail_silently=True)
        User.create_table(fail_silently=True)
        Group.create_table(fail_silently=True)
        Userai.create_table(fail_silently=True)
        Aikey.create_table(fail_silently=True)
        Joke.create_table(fail_silently=True)
        Quotes.create_table(fail_silently=True)
        Logs.create_table(fail_silently=True)
    #create_table()
    def create_Quotes():
        def hito():
            print(time.time())
            data=json.loads(util.get("http://api.hitokoto.us/rand"))
            v,k=data["hitokoto"],data['catname']
            return v,k
        while 1:
            try:
                v,k=hito()
                try:
                    Quotes.get(Quotes.data==v,Quotes.type==k)
                except Quotes.DoesNotExist:
                    Quotes.create(data=v,type=k)
                    print(v,k)
                #input()
            except Exception as e:
                print(e)
    create_Quotes()
    def create_joke():
        while 1:
            try:
                print(time.time())
                data=util.post("http://www.xiaohuangji.com/ajax.php",{'para':'笑话'})
                try:
                    Joke.get(Joke.data==data)
                except Joke.DoesNotExist:
                    Joke.create(data=data)
                    print(data)
                #input()
            except Exception as e:
                print(e)
    #create_joke()
    def read_Quotes():
        for v in open('mrmy.txt','r').readlines():
            k=''
            if v:
                try:
                    Quotes.get(Quotes.data==v,Quotes.type==k)
                except Quotes.DoesNotExist:
                    Quotes.create(data=v,type=k)
                    print(v,k)
                #input()
            #except Exception as e:
            #    print(e)

   #read_Quotes()



